package com.zys.el.time.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zys.el.time.common.exception.sql.SqlException;
import com.zys.el.time.entity.Collect;
import com.zys.el.time.entity.ThinkingLike;
import com.zys.el.time.mapper.CollectMapper;
import com.zys.el.time.service.CollectService;
import org.springframework.stereotype.Service;


@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
    implements CollectService {
    @Override
    public boolean isCollect(Long thinkingId, Long userId) {
        Long count = this.query().eq("thinking_id", thinkingId).eq("user_id", userId).count();
        return count > 0;
    }

    @Override
    public void changeCollectStatus(Long thinkingId, Long userId, boolean flag) {
        QueryWrapper<ThinkingLike> qw = new QueryWrapper<>();
        boolean b = this.update().eq("thinking_id", thinkingId)
                .eq("user_id", userId)
                .setSql("is_delete = " + (flag ? 0 : 1)).update();
        if (!b) {
            throw new SqlException("修改收藏状态异常");
        }

    }
}




